package me.zhengjie.spider.util;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ReUtil;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.video.VideoRuleBean;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class TestSpider implements PageProcessor {

    private Site site = Site.me().setRetryTimes(10).setSleepTime(1000).setTimeOut(30000)
            .addHeader("Accept-Encoding", "/").setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36");

    @Override
    public void process(Page page) {
        // 视频
//        page.putField(SpiderConstant.IMG, page.getHtml().xpath("//div[@class='col-xlg videos-item']/div/div[1]/a/img/@data-original").all());
//        page.putField(SpiderConstant.HREF, page.getHtml().xpath("//div[@class='col-xlg videos-item']/div/div[1]/a/@href").all());
//        page.putField(SpiderConstant.ALT, page.getHtml().xpath("//div[@class='col-xlg videos-item']/div/div[2]/div[1]/a/text()").all());
//        page.putField(SpiderConstant.TYPE, page.getHtml().xpath("/html/body/section/div[2]/div[2]/div[4]/div/div[2]/span[1]/a/text()").toString());
//        page.putField(SpiderConstant.TAG, page.getHtml().xpath("//div[@class='video-metas video-metas-tag']/p/a/button/text()").all());
//        page.putField(SpiderConstant.PLAY_LINK, page.getHtml().regex("http[s]?://[[[\\w]*[-]?]*[.]?]*[[/]?[[\\w]*[\\d]*]*]*[.]?m3u8").all());
//        page.addTargetRequests(page.getHtml().regex("/videos/index/[0-9]*").all());
//        page.putField(SpiderConstant.HREF, page.getHtml().xpath("//div[@class='row col5 clearfix']/dl").toString());
//        page.putField(SpiderConstant.HREF, page.getHtml().xpath("//div[@class='section__grid section__grid--preview video_list add_stats 0']/ul/li/div/a/@href").all());
//        page.putField(SpiderConstant.IMG, page.getHtml().xpath("//div[@class='section__grid section__grid--preview video_list add_stats 0']/ul/li/div/a/img/@data-src").all());
//        page.putField(SpiderConstant.ALT, page.getHtml().xpath("//i[@class='fa fa-angle-double-right']/../@href").toString());
        page.putField(SpiderConstant.NEXT_PAGE, page.getHtml().xpath("//ul[@class='menu-bolock-ul']/li[8]/a/@href").toString());
        page.putField(SpiderConstant.IMG_LIST, page.getHtml().xpath("//div[@class='row thumb-overlay-albums']/div/img/@data-original").all());
//        page.putField(SpiderConstant.ALT, page.getHtml().xpath("//div[@class='section__grid section__grid--preview video_list add_stats 0']/ul/li/div/h3/a/text()").all());
//        page.putField(SpiderConstant.TARGET_LINK, page.getHtml().regex("/categories/all/[0-9]*").all());
//        page.putField(SpiderConstant.ALT, page.getHtml().xpath("//div[@class='pure-u-1 navigations page bdcor bsz lhgt']/ul/li/a/@href").toString());

        // 漫画
//        System.out.println(page.getHtml().regex("https://jm-comic3.art/albums\\?o=mr\\&amp;page=[\\d]*").all().size());
//        System.out.println(page.getHtml().regex("page=[\\d]*").all().size());
//        page.putField("targetLink", page.getHtml().xpath("//*[@id='wrapper']/div[4]/div[6]/ul/li/a/@href").all());
//        page.putField("href", page.getHtml().xpath("//div[@class='p-b-15 p-l-5 p-r-5']/div[1]/a/@href").all());
//        page.putField("img", page.getHtml().xpath("//div[@class='p-b-15 p-l-5 p-r-5']/div[1]/a/img/@src").all());
//        page.putField("alt", page.getHtml().xpath("//div[@class='p-b-15 p-l-5 p-r-5']/div[1]/a/img/@title").all());


//        page.addTargetRequests(page.getHtml().xpath("//*[@id=\"wrapper\"]/div[4]/div[6]/ul/li/a/@href").all());
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {


        // 文件下载
//        FileReader fileReader = new FileReader("E:\\spider\\eladmin\\index.m3u8");
//        List<String> strings = fileReader.readLines();
//        List<String> s = strings.stream().map(item -> {
//            if (item.startsWith("F:\\WorkSpace\\Java\\eladmin\\eladmin-system\\target\\classes\\")) {
//                item = StrUtil.removePrefix(item, "F:\\WorkSpace\\Java\\eladmin\\eladmin-system\\target\\classes\\");
//            }
//            if (ReUtil.isMatch("[\\w]+.ts", item)) {
//                item = "https://cdn.aqd-tv.com/video/015a357e/playlist/" + item;
//            }
//            return item;
//        }).collect(Collectors.toList());
//        FileWriter writer = new FileWriter("E:\\spider\\eladmin\\index.m3u8");
//        writer.writeLines(s);
        // 爬虫测试
        Spider.create(new TestSpider()).addUrl("https://jm-comic3.art/photo/390372").thread(1).run();


        // 正则表达式
        // https://www.904b6.com/video/zipai
        // https://www.904b6.com/video/zipai/index.html
//        String line = "background-image: url('https://img.img1yutu.com:3451/upload/vod/20230719-1/061647ba484bf4273a2d0d3ef1b019d1.jpg')";
//        String pattern = SpiderConstant.JPG_REGEX_XX;
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(line);
//        if (m.find( )) {
//            System.out.println("Found value: " + m.group(0) );
//        } else {
//            System.out.println("NO MATCH");
//        }
    }

    /**
     * @param playLink   https://cdn.aqd-tv.com/video/7e3c9927/playlist/7e3c9927.m3u8
     * @param targetFile E:\\spider\\eladmin\\index.m3u8
     * @param tsName     ts文件名匹配规则
     */
    public void overwriteM3u8File(String playLink, String targetFile, String tsName) {
        File file = new File(playLink);
        String prePath = file.getParentFile().getPath() + SpiderConstant.FOLDER_DIR;
        FileReader fileReader = new FileReader(targetFile);
        List<String> strings = fileReader.readLines();
        List<String> outputList = strings.stream().map(item -> {
            if (ReUtil.isMatch(tsName, item)) {
                item = prePath + item;
            }
            return item;
        }).collect(Collectors.toList());
        FileWriter writer = new FileWriter(targetFile);
        writer.writeLines(outputList);
    }

    public static VideoRuleBean buildSpiderRuleBean() {
        VideoRuleBean videoRuleBean = new VideoRuleBean();
        videoRuleBean.setAlt("//div[@class='col-xlg videos-item']/div/div[2]/div[1]/a/text()");
        videoRuleBean.setImg("//div[@class='col-xlg videos-item']/div/div[1]/a/img/@data-original");
        videoRuleBean.setHref("//div[@class='col-xlg videos-item']/div/div[1]/a/@href");
        videoRuleBean.setTargetLink("/videos/index/[0-9]*");
        videoRuleBean.setType("/html/body/section/div[2]/div[2]/div[4]/div/div[2]/span[1]/a/text()");
        videoRuleBean.setTag("/html/body/section/div[2]/div[2]/div[4]/div/div[4]/p/a/button/text()");
        videoRuleBean.setPlayLink("http[s]?://[[[\\\\w]*[-]?]*[.]?]*[[/]?[[\\\\w]*[\\\\d]*]*]*[.]?m3u8");
        videoRuleBean.setMainLink("https://vip.aqdw196.com/");
        return videoRuleBean;
    }
}
